home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / moonstonemadness.swf / scripts / __Packages / Library / Sound / SoundItem.as next >
Text File  |  2007-09-27  |  5KB  |  194 lines

  1. class Library.Sound.SoundItem
  2. {
  3.    static var FADE_RATE = 8;
  4.    static var FADE_AT_END_TIME = 800;
  5.    function SoundItem(__sndObject, __sLinkage, __nVolume, __nLoop, __sCategory, __mc)
  6.    {
  7.       this.sndObj = __sndObject;
  8.       this.sLinkage = __sLinkage;
  9.       this.mcRef = __mc;
  10.       this.nRemainingLoop = __nLoop;
  11.       this.nCurrentVolume = __nVolume;
  12.       this.nTargetVolume = __nVolume;
  13.       this.nFadeRate = Library.Sound.SoundItem.FADE_RATE;
  14.       this.sCategory = __sCategory;
  15.       this.bFadeAtEnd = false;
  16.       this.bMuted = false;
  17.       this.bPaused = false;
  18.       this.bNeedFreshStart = false;
  19.       this.sndObj.onSoundComplete = Library.Utils.Delegate.create(this,this.doSoundComplete);
  20.       this.sndObj.start(0,__nLoop);
  21.       this.sndObj.setVolume(this.returnComputedVolume(this.nCurrentVolume));
  22.       this.nCurrentTime = 0;
  23.       this.aEventListeners = new Array();
  24.    }
  25.    function doEnterFrame()
  26.    {
  27.       if(!this.bPaused)
  28.       {
  29.          if(this.nCurrentTime > this.sndObj.position)
  30.          {
  31.             this.nRemainingLoop = this.nRemainingLoop - 1;
  32.          }
  33.          this.nCurrentTime = this.sndObj.position;
  34.       }
  35.       this.doCheckFadeAtEnd();
  36.       this.doManageFade();
  37.    }
  38.    function doAddListener(__oListener)
  39.    {
  40.       this.aEventListeners.push(__oListener);
  41.    }
  42.    function doRemoveListener(__oListener)
  43.    {
  44.       var _loc2_ = 0;
  45.       while(_loc2_ < this.aEventListeners.length)
  46.       {
  47.          if(this.aEventListeners[_loc2_] == __oListener)
  48.          {
  49.             delete this.aEventListeners[_loc2_];
  50.             this.aEventListeners.splice(_loc2_,1);
  51.          }
  52.          _loc2_ = _loc2_ + 1;
  53.       }
  54.    }
  55.    function doSoundComplete()
  56.    {
  57.       if(this.bNeedFreshStart && this.nRemainingLoop > 1)
  58.       {
  59.          this.sndObj.start(0,this.nRemainingLoop - 1);
  60.       }
  61.       else
  62.       {
  63.          this.doManageEndEvent();
  64.       }
  65.    }
  66.    function doUpdateSound()
  67.    {
  68.       this.sndObj.setVolume(this.returnComputedVolume(this.nCurrentVolume));
  69.    }
  70.    function doMute()
  71.    {
  72.       this.bMuted = true;
  73.       this.doUpdateSound();
  74.    }
  75.    function doUnMute()
  76.    {
  77.       this.bMuted = false;
  78.       this.doUpdateSound();
  79.    }
  80.    function doPause()
  81.    {
  82.       this.sndObj.stop();
  83.       this.bPaused = true;
  84.    }
  85.    function doResume()
  86.    {
  87.       this.bPaused = false;
  88.       this.bNeedFreshStart = true;
  89.       this.sndObj.start(this.nCurrentTime / 1000,1);
  90.    }
  91.    function doStop()
  92.    {
  93.       this.sndObj.stop();
  94.       this.doManageEndEvent();
  95.    }
  96.    function doFadeTo(__nVolume, __bStopAndDelete)
  97.    {
  98.       if(__bStopAndDelete == undefined)
  99.       {
  100.          __bStopAndDelete = true;
  101.       }
  102.       this.bStopAfterFade = __bStopAndDelete;
  103.       this.nTargetVolume = __nVolume;
  104.    }
  105.    function setFadeRate(__nRate)
  106.    {
  107.       if(__nRate == undefined)
  108.       {
  109.          __nRate = Library.Sound.SoundItem.FADE_RATE;
  110.       }
  111.       this.nFadeRate = __nRate;
  112.    }
  113.    function setFadeAtEnd(__bFadeAtEnd)
  114.    {
  115.       this.bFadeAtEnd = true;
  116.    }
  117.    function setPan(__nPan)
  118.    {
  119.       this.sndObj.setPan(__nPan);
  120.    }
  121.    function get Category()
  122.    {
  123.       return this.sCategory;
  124.    }
  125.    function get LinkageName()
  126.    {
  127.       return this.sLinkage;
  128.    }
  129.    function doDestroy()
  130.    {
  131.       this.sndObj.stop();
  132.       delete this.sndObj;
  133.       this.mcRef.removeMovieClip();
  134.    }
  135.    function doCheckFadeAtEnd()
  136.    {
  137.       if(this.bFadeAtEnd)
  138.       {
  139.          if(this.nRemainingLoop == 1)
  140.          {
  141.             if(this.sndObj.duration - this.nCurrentTime <= Library.Sound.SoundItem.FADE_AT_END_TIME)
  142.             {
  143.                this.doFadeTo(0);
  144.             }
  145.          }
  146.       }
  147.    }
  148.    function doManageEndEvent()
  149.    {
  150.       var _loc2_ = 0;
  151.       while(_loc2_ < this.aEventListeners.length)
  152.       {
  153.          this.aEventListeners[_loc2_].doSoundEvent(Library.Sound.SoundManager.EVENT_SOUND_COMPLETE,this);
  154.          _loc2_ = _loc2_ + 1;
  155.       }
  156.       this.aEventListeners = new Array();
  157.       this.mcRef.removeMovieClip();
  158.       delete this.mcRef;
  159.       delete this.aEventListeners;
  160.       delete this.sndObj.onSoundComplete;
  161.       delete this.sndObj;
  162.    }
  163.    function doManageFade()
  164.    {
  165.       if(this.nCurrentVolume != this.nTargetVolume)
  166.       {
  167.          this.nCurrentVolume = Library.Utils.MoreMath.getReachNum(this.nCurrentVolume,this.nTargetVolume,this.nFadeRate);
  168.          this.sndObj.setVolume(this.returnComputedVolume(this.nCurrentVolume));
  169.       }
  170.       if(this.nCurrentVolume <= 0 && this.bStopAfterFade)
  171.       {
  172.          this.sndObj.stop();
  173.          this.doManageEndEvent();
  174.       }
  175.    }
  176.    function returnComputedVolume(__nVolume)
  177.    {
  178.       var _loc2_ = undefined;
  179.       if(!this.bMuted && !Library.Sound.SoundManager.isCategoryMuted(this.sCategory))
  180.       {
  181.          var _loc3_ = Library.Sound.SoundManager.__get__MasterVolume() / 100;
  182.          var _loc4_ = Library.Sound.SoundManager.getCategoryVolume(this.sCategory) / 100;
  183.          _loc2_ = __nVolume;
  184.          _loc2_ *= _loc4_;
  185.          _loc2_ *= _loc3_;
  186.       }
  187.       else
  188.       {
  189.          _loc2_ = 0;
  190.       }
  191.       return _loc2_;
  192.    }
  193. }
  194.